#include "GlCommon.h"

#include "GlMatrix4x4.h"
#include "GlPosition.h"
#include "GlVector4.h"
#include "Utility.h"

namespace GraphicLibrary
{
	//---------------------------------------------------
	float getAngleTo(const Position &origin, const Position &target)
	{
		Position dif(target.mX - origin.mX, target.mY - origin.mY);
		if(dif.mX == 0.0f && dif.mY == 0.0f)
			throw CelParameterError;
		if(dif.mX == 0.0f)
			return dif.mY > 0.0f
						? Utility::Pi * 0.5f
						: Utility::Pi * -0.5f;
		else
		{
			float a = dif.mY / dif.mX;
			float angle = atan(a);
			if(dif.mX < 0.0f)
				angle += Utility::Pi;
			return angle;
		}
	}

	//---------------------------------------------------
	Position shiftPos(const Position &pos, const float direction, const float distance)
	{
		return Position(pos.mX + cos(direction) * distance, pos.mY + sin(direction) * distance);
	}

	//---------------------------------------------------
	Position operator * (const Position &pos, const Matrix4x4 &mat)
	{
		Vector4 v;
		v(0) = pos.mX;
		v(1) = pos.mY;
		v(2) = 0.0f;
		v(3) = 1.0f;

		v = v * mat;
		return Position(v(0), v(1));
	}

	//---------------------------------------------------
	float rand(const float maxValue)
	{
		return maxValue * static_cast<float>(::rand()) / static_cast<float>(RAND_MAX);
	}
}
